package com.landiao.ctrler.management;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.landiao.ctrler.BaseCRUDCtrler;
import com.landiao.entities.Package;
import com.landiao.entities.ServiceItem;
import com.landiao.entities.ServiceItemPrice;
import com.landiao.entities.ShopGrade;
import com.landiao.service.PackageService;
import com.landiao.service.ServiceItemService;
import com.landiao.service.ShopService;

@Controller
@RequestMapping("/management/package")
public class PackageCtrler extends BaseCRUDCtrler<Package, String> {
	
	@Autowired
	private ServiceItemService serviceItemService;
	
	@Autowired
	private ShopService shopService;

	private PackageService getPackageService() {
        return (PackageService) baseService;
    }

	@Override
	public String add(Model model) {
		// TODO Auto-generated method stub
		List<ServiceItem> items = this.serviceItemService.getAllNormalServiceItem(null);
		model.addAttribute("items", items);
		
		List<ShopGrade> shopGrades = this.shopService.getListshopGrade(null);
		model.addAttribute("shopGrades", shopGrades);
		
		return super.add(model);
	}
	
	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(Model model,@PathVariable String id) {
		// TODO Auto-generated method stub
		addModelAttr(id,model);
		model.addAttribute("op", "修改");
		return viewName("edit");
	}
	
	@RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
	public String detail(Model model,@PathVariable String id) {
		// TODO Auto-generated method stub
		addModelAttr(id,model);
		model.addAttribute("op", "详情");
		return viewName("edit");
	}
	
	/**
	 * 修改和详情页面的返回参数
	 * @param id
	 * @param model
	 */
	private void addModelAttr(String id,Model model){
		List<ShopGrade> shopGradeList = this.shopService.getListshopGrade(null);
//		List<Map<String, Object>> priceList = new ArrayList<>();
		Package t = this.getPackageService().getById(id);
		if(t!=null){
			List<ServiceItemPrice> list = this.getPackageService().getItemPriceByPackageId(id);
			Map<String, List<Map<String, Object>>> map = new LinkedHashMap<>();
			for(ServiceItemPrice price:list){
				List<Map<String, Object>> priceList = map.get(price.getServiceItemId());
				if(priceList==null){
					priceList = new ArrayList<>();
					map.put(price.getServiceItemId(), priceList);
				}
				Map<String, Object> tmpMap = new LinkedHashMap<>();
				tmpMap.put("price", price);
				priceList.add(tmpMap);
			}
			//转换成前台参数
			for(Map.Entry<String, List<Map<String, Object>>> entry:map.entrySet()){
				String name = null;
				for(int i=0;i<shopGradeList.size();i++){
					ShopGrade grade = shopGradeList.get(i);
					boolean flag = false;//判断是否已添加过
					for(Map<String, Object> value:entry.getValue()){
						ServiceItemPrice price = (ServiceItemPrice)value.get("price");
						if(price!=null){
							if(name == 	null)
								name = price.getServiceItemName();
							if(price.getShopGradeId().equals(grade.getShopGradeId())){
								value.put("grade", grade);
								flag = true;
								break;
							}
						}
					}
					if(!flag){
						Map<String, Object> tmpMap = new LinkedHashMap<>();
						tmpMap.put("grade", grade);
						tmpMap.put("itemId", entry.getKey());
						tmpMap.put("itemName", name);
						entry.getValue().add(i, tmpMap);
					}
				}
			}
			
//			model.addAttribute("selectItem", map.keySet());
			model.addAttribute("priceList", map);
		}
		model.addAttribute("t",t);
		model.addAttribute("shopGrades", shopGradeList);
		model.addAttribute("shopGradesSize", shopGradeList.size());
		List<ServiceItem> items = this.serviceItemService.getAllNormalServiceItem(null);
		model.addAttribute("items", items);
	}
	
	/**
	 * 验证是否存在
	 * 
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(Package p) {
		String id = p.getPackageId();
		if(StringUtils.isNotEmpty(id));{
			p.setPackageId(null);
		}
		Package pa = this.getPackageService().getOne(p);
		if (pa == null) {
			return true;
		}else{
			if(pa.getPackageId().equals(id)){
				return true;
			}else{
				return false;
			}
		}
	}
}
